package com.lagou.edu.sss.controller;

import com.lagou.edu.sss.pojo.Resume;
import com.lagou.edu.sss.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/list")
    public ModelAndView findAll() {
        List<Resume> resumes = resumeService.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("resumes", resumes);
        modelAndView.setViewName("resume");
        return modelAndView;
    }

    @RequestMapping(value = {"/{id}", ""}, method = RequestMethod.GET)
    public ModelAndView viewDetailOrCreate(@PathVariable(required = false) Long id) {
        ModelAndView modelAndView = new ModelAndView();
        Resume resume = null;
        if (id != null) {
            resume = resumeService.findById(id);
        }
        modelAndView.addObject("resume", resume);
        modelAndView.setViewName("create");
        return modelAndView;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String save(@ModelAttribute Resume resume) {
        resumeService.save(resume);
        return "redirect:/resume/list";
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable Long id) {
        resumeService.delete(id);
    }

}
